import datetime
import sys

import win32com.client as win32

import os
import stat
import time




class 数据处理:
    # current_date = datetime.date.today()
    #
    # # 指定日期
    # specified_date = datetime.date(2024, 5, 9)
    #
    # # 判断当前日期是否大于指定日期
    # if current_date > specified_date:
    #     sys.exit("当前日期大于{}，程序终止".format(specified_date))
    def make_excel_files_writable(self,folder_path):

        excel_files = [file for file in os.listdir(folder_path) if file.endswith(('.xls', '.xlsx'))]


        for file in excel_files:
            file_path = os.path.join(folder_path, file)

            if not os.access(file_path, os.W_OK):
                try:

                    os.chmod(file_path, stat.S_IWRITE)

                except Exception as e:
                    print(f"无法修改文件 {file} 的属性。错误：{e}")
            else:
                pass



    def office(self):


        folder_path = input("请输入文件全路径")  # 指定文件夹路径
        xl = win32.gencache.EnsureDispatch('Excel.Application')

        xl.Visible = True
        xl.DisplayAlerts = 0


        files = os.listdir(folder_path)

        for file in files:

            full_path = os.path.join(folder_path, file)
            print(full_path)
            xlbook = xl.Workbooks.Open(full_path)

            print("正在处理" + full_path)

            try:
                xlmodule = xlbook.VBProject.VBComponents("我的模块")
            except:
                xlmodule = xlbook.VBProject.VBComponents.Add(1)
                xlmodule.Name = '我的模块'
                code = '''
                Sub MyVBA()
                Dim shp As Shape

                For Each shp In ActiveSheet.Shapes
                    shp.Delete
                Next shp


                End Sub

                '''
                xlmodule.CodeModule.AddFromString(code)

            for sheet in xlbook.Sheets:
                try:
                    sheet.Activate()
                    xlbook.Application.Run('我的模块.MyVBA')

                except Exception as e:
                    print("Error:", e)

            if (xlbook.VBProject.VBComponents("我的模块")):
                xlbook.VBProject.VBComponents.Remove(xlmodule)
            time.sleep(1)
            xlbook.Save()
            time.sleep(1)
            xlbook.Close()
            xl.Quit()

    def wps(self):



        folder_path = input("请输入文件全路径")  # 指定文件夹路径
        self.make_excel_files_writable(folder_path)
        xl = win32.gencache.EnsureDispatch('Ket.Application')

        xl.Visible = False
        xl.DisplayAlerts = 0


        files = os.listdir(folder_path)

        for file in files:

            full_path = os.path.join(folder_path, file)
            print(full_path)
            xlbook = xl.Workbooks.Open(full_path)

            print("正在处理" + full_path)

            time.sleep(2)
            try:
                xlmodule = xlbook.VBProject.VBComponents("我的模块")
            except:
                xlmodule = xlbook.VBProject.VBComponents.Add(1)
                xlmodule.Name = '我的模块'
                code = '''
                Sub MyVBA()
                Dim shp As Shape

                For Each shp In ActiveSheet.Shapes
                    shp.Delete
                Next shp


                End Sub

                '''
                xlmodule.CodeModule.AddFromString(code)

            for sheet in xlbook.Sheets:
                try:
                    sheet.Activate()
                    xlbook.Application.Run('我的模块.MyVBA')

                except Exception as e:
                    print("Error:", e)

            if (xlbook.VBProject.VBComponents("我的模块")):
                xlbook.VBProject.VBComponents.Remove(xlmodule)
            time.sleep(1)
            xlbook.Save()
            time.sleep(1)
            xlbook.Close()
            xl.Quit()
    def start(self):


        try:

            win32.Dispatch("ket.Application")  # "kwps.Application" 是 WPS 表格的 ProgID
            print("正在使用wps处理")
            数据处理().wps()


        except:
            print("没有wps,将检测使用office")

            数据处理().office()


#




数据处理().wps()